package cn.itcast.bos.web.action;

import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.util.HashMap;
import java.util.Map;

import javax.ws.rs.core.MediaType;

import org.apache.commons.io.FileUtils;
import org.apache.cxf.jaxrs.client.WebClient;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionContext;

import cn.itcast.bos.constant.Constants;
import cn.itcast.bos.domain.page.PageBean;
import cn.itcast.bos.domain.take_delivery.Promotion;
import cn.itcast.bos.web.action.common.BaseAction;
import freemarker.template.Configuration;
import freemarker.template.Template;

@ParentPackage("json-default")
@Namespace("/")
@Controller
@Scope("prototype")
public class PromotionAction extends BaseAction<Promotion> {

	// 属性驱动获取页面传递的参数，参数在page.js 中的
	/*
	 * params: { "page": page, "rows": $scope.pageSize }
	 */
	private Integer page;
	private Integer rows;

	public void setPage(Integer page) {
		this.page = page;
	}

	public void setRows(Integer rows) {
		this.rows = rows;
	}

	@Action(value="promotion_pageQuery",results={@Result(name="success",type="json")})
	public String pageQuery() {
		//调用bos_management系统查询分页数据
		PageBean pageData = WebClient.create(Constants.BOS_MANAGEMENT_URL+"/services/promotionService/findPageDate?page="+page+"&rows="+rows)
				.type(MediaType.APPLICATION_JSON)
				.accept(MediaType.APPLICATION_JSON)
				.get(PageBean.class);
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("totalCount", pageData.getTotalElements());//map里存放的是传递到页面的json数据，因此键的命名要和页面取值时用的名字一样
		map.put("pageData", pageData.getContent());
		ActionContext.getContext().getValueStack().push(map);
		return SUCCESS;
	}
	
	//生成静态页面
	@Action(value="promotion_showDetail")
	public String showDetail() throws Exception {
		//先判断id，对应的html是否存在，如果存在直接返回
		//查找freemarker文件夹的路径
		String htmlRealPath = ServletActionContext.getServletContext().getRealPath("/freemarker");
		File htmlFile = new File(htmlRealPath+"/"+model.getId()+".html");
		if(!htmlFile.exists()) {
			Configuration configuration = new Configuration(Configuration.VERSION_2_3_22);
			configuration.setDirectoryForTemplateLoading(new File(ServletActionContext.getServletContext().getRealPath("/WEB-INF/freemarker_template")));
			// 获取模板对象
			Template template = configuration.getTemplate("promotion_detail.ftl");
			// 动态数据获取
			Promotion promotion = WebClient.create(Constants.BOS_MANAGEMENT_URL+"/services/promotionService")
				.path("/findDetailById/"+model.getId())
				.type(MediaType.APPLICATION_JSON)
				.accept(MediaType.APPLICATION_JSON)
				.get(Promotion.class);
			//组织数据
			Map<String,Object> map = new HashMap<String,Object>();
			map.put("promotion", promotion);
			
			template.process(map, new OutputStreamWriter(new FileOutputStream(htmlFile),"utf-8"));

		}
		//文件存在，直接返回
		ServletActionContext.getResponse().setContentType("text/html;charset=utf-8");
		FileUtils.copyFile(htmlFile, ServletActionContext.getResponse().getOutputStream());
		return NONE;
	}
}
